libname cdi "S:\Korte\Ivory_Coast_2015\l'analyse des donnees_Hyer\Donnees";
libname acdi "S:\Korte\Ivory_Coast_2015\l'analyse des donnees_Hyer\Archive\Clinical datasets to combine for analysis_2017_02_07";
libname psm "S:\Korte\Ivory_Coast_2015\propensity score matching";

PROC IMPORT OUT=IC_1_
			DATAFILE= "S:\Korte\Ivory_Coast_2015\l'analyse des donnees_Hyer\Archive\Clinical datasets to combine for analysis_2017_02_07\3ie Patient Files (PMID only)_12132016.xlsx" 
			DBMS=xlsx REPLACE;
     GETNAMES=YES; 
run;
PROC IMPORT OUT=IC_2_
			DATAFILE= "S:\Korte\Ivory_Coast_2015\l'analyse des donnees_Hyer\Archive\Clinical datasets to combine for analysis_2017_02_07\2016_11_28 CABE FdS results to Jeff.xlsx" 
			DBMS=xlsx REPLACE;
     GETNAMES=YES; 
run;
PROC IMPORT OUT=IC_3_
			DATAFILE= "S:\Korte\Ivory_Coast_2015\l'analyse des donnees_Hyer\Archive\Clinical datasets to combine for analysis_2017_02_07\Copy of Copy of All Patient Files_JoinedTo Send_GF_AM_GF.xlsx" 
			DBMS=xlsx REPLACE;
     GETNAMES=YES; 
run;
PROC IMPORT OUT=IC_4_
			DATAFILE= "S:\Korte\Ivory_Coast_2015\l'analyse des donnees_Hyer\Archive\Clinical datasets to combine for analysis_2017_02_07\CPIM Fiche de Suivi Data_reconciling_12.1.xlsx" 
			DBMS=xlsx REPLACE;
     GETNAMES=YES; 
run;
/*
PROC IMPORT OUT=IC_5_
			DATAFILE= "S:\Korte\Ivory_Coast_2015\l'analyse des donnees_Hyer\Archive\Clinical datasets to combine for analysis_2017_02_07\FINAL REDCap_data_for analysis_12.1.xlsx" 
			DBMS=xlsx REPLACE;
     GETNAMES=YES; 
run;
*/
data IC_5_;
      set acdi.finaltemp;
run;
PROC IMPORT OUT=IC_6_
			DATAFILE= "S:\Korte\Ivory_Coast_2015\l'analyse des donnees_Hyer\Archive\Clinical datasets to combine for analysis_2017_02_07\Patient Files_GF_NDAD_rest_To Send_AM.xlsx" 
			DBMS=xlsx REPLACE;
     GETNAMES=YES; 
run;

data IC_1;
	set IC_1_;
	site = substr(record_id, 3, 4);
	ID = record_id;
	if sexe_1 = 'Feminin' then Feminin = 1;
		else if sexe_1 = 'Masculin' then Feminin = 0;
run;
data IC_2;
	set IC_2_;
	site = substr(record_id_2, 3, 4);
	ID = record_id_2;
	if sexe_1 = 'Feminin' then Feminin = 1;
		else if sexe_1 = 'Masculin' then Feminin = 0;
run;
data IC_3;
	set IC_3_;
	site = substr(Study_ID, 3, 4);
	ID = Study_ID;
	if sexe_1 = 'Feminin' then Feminin = 1;
		else if sexe_1 = 'Masculin' then Feminin = 0;
run;
data IC_4;
	set IC_4_;
	site = substr(record_id_2, 3, 4);
	ID = record_id_2;
	if sexe_1 = 'Feminin' then Feminin = 1;
		else if sexe_1 = 'Masculin' then Feminin = 0;
run;
data IC_5;
	set IC_5_;
	site = substr(record_id_2, 3, 4);
	ID = record_id_2;
	if sexe_1 = 2 then Feminin = 1; ********************************CHECK THIS;
		else if sexe_1 = 1 then Feminin = 0;
	if site in('CABE' 'CPIM') and outil_1_abstraction_de_dossier_c gt '18oct2015:00:23:59'dt then keep = 0;
	if keep ^= 0;
run;
data IC_6;
	set IC_6_;
	site = substr(record_id, 3, 4);
	ID = record_id;
	if sexe_1 = 'Feminin' then Feminin = 1;
		else if sexe_1 = 'Masculin' then Feminin = 0;
run;
data IC_7;
	set acdi.teams_abcd_072016;
	site = substr(record_id, 3, 4);
	ID = record_id;
	if sexe_1 = 2 then Feminin = 1; ********************************CHECK THIS;
		else if sexe_1 = 1 then Feminin = 0;
run;

options mprint;
%Macro Convert;
%do i = 1 %to 7;
	data DC_IC_&i.;
		set IC_&i.;
		ios_resultant_tb = ios_resultant_tb_1 + 0;
		annee_naissance = annee_nasissance_1 + 0;
		poids_kg		= poids_kg_1 + 0;		
		traitement_poids_kg = traitement_poids_kg_1 + 0;
		cd4_jour0 		= cd4_jour0_1 + 0;
		cdc_partie1		= cdc_partie1_1 + 0;
		cdc_partie2		= cdc_partie2_1 + 0;
		annee_testvih	= annee_testvih_1 + 0;
		matri			= matri_1 + 0;
		suivi_visite_v0	= suivi_visite_1 + 0;
		date_rendez_vous_v0 = date_rendez_vous_1 + 0;
		%do ii = 0 %to 40;
		%let ii_1 = %eval(&ii. + 1);
			suivi_visite_v&ii_1.			 	= fv&ii._suivi_visite_1 			+ 0;
			date_rendez_vous_v&ii_1. 			= fv&ii._date_rendez_vous_1 		+ 0;
			constantes_poids_kg_v&ii_1. 		= fv&ii._constantes_poids_kg_1 		+ 0;
			constantes_ta_dias_v&ii_1. 			= fv&ii._constantes_ta_dias_1 		+ 0;
			constantes_ta_sys_v&ii_1.			= fv&ii._constantes_ta_sys_1 		+ 0;		
			contraception_cocher_v&ii_1.		= fv&ii._contraception_cocher_1 	+ 0;		
			date_rendez_vous_v&ii_1.			= fv&ii._date_rendez_vous_1 		+ 0;			
			dernier_cd4_date_v&ii_1.			= fv&ii._dernier_cd4_date_1 		+ 0;			
			dernier_mm3_v&ii_1. 				= fv&ii._dernier_mm3_1  			+ 0;				
			duree_debut_arv_v&ii_1.				= fv&ii._duree_debut_arv_1	 		+ 0;		
			ios_arv_v&ii_1.						= fv&ii._ios_arv_1		 			+ 0;			
			ios_contraception_cours_v&ii_1. 	= fv&ii._ios_contraception_cours_1  + 0;	
			ios_grossesse_cours_v&ii_1. 		= fv&ii._ios_grossesse_cours_1  	+ 0;		
			ios_resultant_tb_v&ii_1.			= fv&ii._ios_resultant_tb_1	 		+ 0;		
			ios_traitement_tb_v&ii_1.			= fv&ii._ios_traitement_tb_1	 	+ 0;		
			jours_debut_arv_v&ii_1.				= fv&ii._jours_debut_arv_1 			+ 0;		
			mois_debut_arv_v&ii_1.				= fv&ii._mois_debut_arv_1	 		+ 0;	
			pourcent_der_mm3_v&ii_1.			= fv&ii._pourcent_der_mm3_1b	 	+ 0;		
			stade_cdc_v&ii_1.  					= fv&ii._stade_cdc_1  		 		+ 0;		
			stade_oms_v&ii_1. 					= fv&ii._stade_oms_1  		 		+ 0;
		%end;
		%let var_dates = 1;
		%do %until(not %length(%scan(&Dates,&var_dates)));
			%scan(&Dates,&var_dates)_dc = %scan(&Dates,&var_dates) + 0;
		%let var_dates = %eval(&var_dates + 1);
		%end;
		%let DemosChr = 1;
		%do %until(not %length(%scan(&VarsSansDateChr,&DemosChr)));
			%scan(&VarsSansDateChr,&DemosChr)_dc = put(%scan(&VarsSansDateChr,&DemosChr),20.);
		%let DemosChr = %eval(&DemosChr + 1);
		%end;
		keep &VarsSansDateNum. site ID Feminin ios_resultant_tb annee_naissance
			suivi_visite_v0-suivi_visite_v41				/*jeff changed these all to v0 instead of v1*/
			date_rendez_vous_v0-date_rendez_vous_v41 		/*                   and to v41 instead of v40*/
			constantes_poids_kg_v0-constantes_poids_kg_v41
			constantes_ta_dias_v0-constantes_ta_dias_v41 		
			constantes_ta_sys_v0-constantes_ta_sys_v41			
			contraception_cocher_v0-contraception_cocher_v41	
			dernier_cd4_date_v0-dernier_cd4_date_v41			
			dernier_mm3_v0-dernier_mm3_v41 				
			duree_debut_arv_v0-duree_debut_arv_v41			
			ios_arv_v0-ios_arv_v41					
			ios_contraception_cours_v0-ios_contraception_cours_v41	
			ios_grossesse_cours_v0-ios_grossesse_cours_v41 		
			ios_resultant_tb_v0-ios_resultant_tb_v41		
			ios_traitement_tb_v0-ios_traitement_tb_v41			
			jours_debut_arv_v0-jours_debut_arv_v41			
			mois_debut_arv_v0-mois_debut_arv_v41			
			pourcent_der_mm3_v0-pourcent_der_mm3_v41			
			stade_cdc_v0-stade_cdc_v41				
			stade_oms_v0-stade_oms_v41
			%let var_dates = 1;
			%do %until(not %length(%scan(&Dates,&var_dates)));
				%scan(&Dates,&var_dates)_dc
			%let var_dates = %eval(&var_dates + 1);
			%end;
			%let DemosChr = 1;
			%do %until(not %length(%scan(&VarsSansDateChr,&DemosChr)));
				%scan(&VarsSansDateChr,&DemosChr)_dc
			%let DemosChr = %eval(&DemosChr + 1);
			%end;
			;
	run;
%end;
data Etape1;
	set DC_IC_1-DC_IC_7;
	if ID notin('I-BETT-1-141' 'I-CPIM-1-010' 'I-CPIM-1-010' 'I-CPIM-1-036' 'I-CPIM-1-059' 'I-CPIM-1-069' '');
run;
%Mend;

%let VarsSansDateNum = 
annee_naissance
ios_resultant_tb
poids_kg
traitement_poids_kg	
matri
cd4_jour0 		
cdc_partie1		
cdc_partie2		
annee_testvih	
matri
Feminin	
;

%let VarsSansDateChr = 
niveau_dinstruction_1
contraception_1
tuberculose_1
etat_fonctionnel_1
resultat_vih_sida_1
beneficie_ptme_1
grossesse_cours_1
point_dentre_soins_1
arv_1
religion_1
profession_1
niveau_dinstruction_1
site
;

%let Dates = 
date_traitementarv_1 
suivi_traitementarv_1
date_abstraction_1
date_dadmission_1
Date_d_admission
suivi_admission_1
suivi_visite_v0	
date_rendez_vous_v0;

%Convert;


%Macro UneLigne;
%let DemosNum = 1;
%do %until(not %length(%scan(&VarsSansDateNum,&DemosNum)));
	data E1_N_&DemosNum.;
		set Etape1;
		if %scan(&VarsSansDateNum,&DemosNum) ^= .;
		keep ID %scan(&VarsSansDateNum,&DemosNum);
	run;
	proc sort data = E1_N_&DemosNum. nodupkey;
		by ID;
	run;
%let DemosNum = %eval(&DemosNum + 1);
%end;
%let Longueur_Demos_N = %eval(&DemosNum - 1);
%let DemosChr = 1;
%do %until(not %length(%scan(&VarsSansDateChr,&DemosChr)));
	data E1_C_&DemosChr.;
		set Etape1;
		if %scan(&VarsSansDateChr,&DemosChr)_dc ^= '';
		%scan(&VarsSansDateChr,&DemosChr) = %scan(&VarsSansDateChr,&DemosChr)_dc;
		keep ID %scan(&VarsSansDateChr,&DemosChr);
	run;
	proc sort data = E1_C_&DemosChr. nodupkey;
		by ID;
	run;
%let DemosChr = %eval(&DemosChr + 1);
%end;
%let Longueur_Demos_C = %eval(&DemosChr - 1);
%let Dates_i = 1;
%do %until(not %length(%scan(&Dates,&Dates_i)));
	data E1_Dt_&Dates_i.;
		set Etape1;
		if %scan(&Dates,&Dates_i)_dc ^= .;
		%scan(&Dates,&Dates_i) = %scan(&Dates,&Dates_i)_dc;
		keep ID %scan(&Dates,&Dates_i);
	run;
	proc sort data = E1_Dt_&Dates_i. nodupkey;
		by ID;
	run;
%let Dates_i = %eval(&Dates_i + 1);
%end;
%let Longueur_Dates = %eval(&Dates_i - 1);
data Etape1_Demos;
	merge E1_N_1-E1_N_&Longueur_Demos_N. E1_C_1-E1_C_&Longueur_Demos_C. E1_Dt_1-E1_Dt_&Longueur_Dates.;
	by ID;
run;
%Mend;

%UneLigne;

options mprint;
%Macro Trier;
%do i = 0 %to 41;
	data Etape1_v&i.;
		set Etape1;
		suivi_visite			 	= suivi_visite_v&i.;;			 	
		date_rendez_vous 			= date_rendez_vous_v&i.;; 			
		constantes_poids_kg 		= constantes_poids_kg_v&i.;; 		
		constantes_ta_dias 			= constantes_ta_dias_v&i.;; 		
		constantes_ta_sys			= constantes_ta_sys_v&i.;;			
		contraception_cocher		= contraception_cocher_v&i.;;		
		date_rendez_vous			= date_rendez_vous_v&i.;;			
		dernier_cd4_date			= dernier_cd4_date_v&i.;;			
		dernier_mm3 				= dernier_mm3_v&i.;; 				
		duree_debut_arv				= duree_debut_arv_v&i.;;			
		ios_arv						= ios_arv_v&i.;;					
		ios_contraception_cours 	= ios_contraception_cours_v&i.;; 	
		ios_grossesse_cours 		= ios_grossesse_cours_v&i.;; 		
		ios_resultant_tb			= ios_resultant_tb_v&i.;;			
		ios_traitement_tb			= ios_traitement_tb_v&i.;;			
		jours_debut_arv				= jours_debut_arv_v&i.;;			
		mois_debut_arv				= mois_debut_arv_v&i.;;			
		pourcent_der_mm3			= pourcent_der_mm3_v&i.;;			
		stade_cdc  					= stade_cdc_v&i.;;  				
		stade_oms 					= stade_oms_v&i.;;
		keep ID suivi_visite date_rendez_vous constantes_poids_kg constantes_ta_dias constantes_ta_sys	contraception_cocher
		dernier_cd4_date dernier_mm3 duree_debut_arv ios_arv ios_contraception_cours ios_grossesse_cours ios_resultant_tb			
		ios_traitement_tb jours_debut_arv mois_debut_arv pourcent_der_mm3 stade_cdc stade_oms;
	run;
%end;
data Etape2_;
	set Etape1_v0-Etape1_v41;
	if suivi_visite ^= .;
run;
proc sort data = Etape2_ nodupkey;
	by ID suivi_visite;
run;
data Etape2;
	set Etape2_;
	Visite + 1;
	by ID;
	if first.ID then Visite = 1;
run;
%Mend;

%Trier;

*ods rtf file="S:\Korte\Ivory_Coast_2015\final report\results 20170223\visits.rtf";
data test;
	 set etape2;
	 by id;
	 if last.id;
	 run;
*proc freq data=test;
*	tables visite;
	run;
*proc freq data = Etape2;
*	table Visite;
run;
*ods rtf close;

%Macro Large;
%do w = 1 %to 22;				*jeff changed this from 21 to 22;
	data Etape2_w_v&w.;
		set Etape2;
		if Visite = &w.;
		suivi_visite_v&w.			 	= suivi_visite;			 	
		date_rendez_vous_v&w. 			= date_rendez_vous; 			
		constantes_poids_kg_v&w. 		= constantes_poids_kg; 		
		constantes_ta_dias_v&w. 		= constantes_ta_dias; 			
		constantes_ta_sys_v&w.			= constantes_ta_sys;			
		contraception_cocher_v&w.		= contraception_cocher;		
		dernier_cd4_date_v&w.			= dernier_cd4_date;			
		dernier_mm3_v&w. 				= dernier_mm3; 				
		duree_debut_arv_v&w.			= duree_debut_arv;				
		ios_arv_v&w.					= ios_arv;						
		ios_contraception_cours_v&w. 	= ios_contraception_cours; 	
		ios_grossesse_cours_v&w. 		= ios_grossesse_cours; 		
		ios_resultant_tb_v&w.			= ios_resultant_tb;			
		ios_traitement_tb_v&w.			= ios_traitement_tb;			
		jours_debut_arv_v&w.			= jours_debut_arv;				
		mois_debut_arv_v&w.				= mois_debut_arv;				
		pourcent_der_mm3_v&w.			= pourcent_der_mm3;			
		stade_cdc_v&w.  				= stade_cdc;  					
		stade_oms_v&w.					= stade_oms;
		keep ID suivi_visite_v&w. date_rendez_vous_v&w. constantes_poids_kg_v&w. constantes_ta_dias_v&w. constantes_ta_sys_v&w. 
		contraception_cocher_v&w. date_rendez_vous_v&w. dernier_cd4_date_v&w. dernier_mm3_v&w. 				
		duree_debut_arv_v&w. ios_arv_v&w. ios_contraception_cours_v&w. ios_grossesse_cours_v&w. ios_resultant_tb_v&w.			
		ios_traitement_tb_v&w. jours_debut_arv_v&w. mois_debut_arv_v&w. pourcent_der_mm3_v&w. stade_cdc_v&w. stade_oms_v&w.;
	run;
	proc sort data = Etape2_w_v&w.;
		by ID;
	run;
%end;
%Mend;

%Large;

proc sort data = Etape1_Demos;
	by ID;
run;

*here is a new one (macro fin) from jeff, trying again;  *2017-02-27;
%macro year;
data year;
	 merge etape1_demos etape2_w_v1-etape2_w_v22;
	 by id;

	 datearvy=year(date_traitementarv_1);
	 suiviarvy=year(suivi_traitementarv_1);

	 %do v=1 %to 22;
	 suivi_vy_v&v. = year(suivi_visite_v&v.);
	 %end;
	 run;
%mend;
%year;
*proc freq
	 data=year;
*	 tables datearvy suiviarvy
			suivi_vy_v1-suivi_vy_v22;
	 run;
%macro fin;
data etape3_mh;
	merge etape1_demos etape2_w_v1-etape2_w_v22;
	by id;
	date_traitementarv_1_dc=date_traitementarv_1;
	 if               .<=date_traitementarv_1<mdy(01,01,2013) then date_traitementarv_1_dc=.;
	 if mdy(07,01,2016)<=date_traitementarv_1<mdy(01,01,2040) then date_traitementarv_1_dc=.;
	 if mdy(01,01,2040)<=date_traitementarv_1<mdy(01,01,3000) then date_traitementarv_1_dc=date_traitementarv_1_dc-21916;
	 if mdy(01,01,3000)<=date_traitementarv_1 then date_traitementarv_1_dc=.;
	suivi_traitementarv_1_dc=suivi_traitementarv_1;
	 if               .<=suivi_traitementarv_1<mdy(01,01,2013) then suivi_traitementarv_1_dc=.;
	 if mdy(07,01,2016)<=suivi_traitementarv_1<mdy(01,01,2040) then suivi_traitementarv_1_dc=.;
	 if mdy(01,01,2040)<=suivi_traitementarv_1<mdy(01,01,3000) then suivi_traitementarv_1_dc=suivi_traitementarv_1_dc-21916;
	 if mdy(01,01,3000)<=suivi_traitementarv_1 then suivi_traitementarv_1_dc=.;
	debut=.;
	 if mdy(01,01,2013)<=suivi_traitementarv_1_dc<mdy(07,01,2016) then debut=suivi_traitementarv_1_dc;
	 if ((debut=.) and (mdy(01,01,2013)<=date_traitementarv_1_dc<mdy(07,01,2016))) then debut=date_traitementarv_1_dc;

	age=year(debut) - annee_naissance;
	ydebut=year(debut);

	if .<age<15 then delete;
	if debut=. then delete;
	%do v = 1 %to 22;
	 if mdy(01,01,2040)<=suivi_visite_v&v.<mdy(01,01,3000) then suivi_visite_v&v.=suivi_visite_v&v.-21916;
	 if                  suivi_visite_v&v.<mdy(01,01,1970) then suivi_visite_v&v. = .; *maybe use a later date like 2005;
	 if mdy(09,01,2016)<=suivi_visite_v&v.                 then suivi_visite_v&v. = .;

	 if mdy(01,01,2040)<=date_rendez_vous_v&v.<mdy(01,01,3000) then date_rendez_vous_v&v.=date_rendez_vous_v&v.-21916;
	 if                  date_rendez_vous_v&v.<mdy(01,01,1970) then date_rendez_vous_v&v. = .; *maybe use a later date like 2005;
	 if mdy(09,01,2016)<=date_rendez_vous_v&v.                 then date_rendez_vous_v&v. = .;

	 suivi_vy_v&v. = year(suivi_visite_v&v.);
	 rdv_y_v&v. = year(date_rendez_vous_v&v.);
	%end;

run;
%mend;

%Fin;



/*
******************************************************************************;
******************************************************************************;
*****************FINAL DATASET FOR ANALYSIS***********************************;
******************************************************************************;
******************************************************************************;

data cdi.donnees_nettoyees_20170227jek;
	set Etape3_mh;
run;
*/




*THE NEXT SECTION CALCULATES RETENTION OUTCOME VARIABLES;

*FIRST--RETENTION USING THE RENDEZVOUS DATE FROM CLINICAL RECORD;

libname cdi "S:\Korte\Ivory_Coast_2015\l'analyse des donnees_Hyer\Donnees";

data Etape1;
	set cdi.donnees_nettoyees_20170227jek;
run;



%Macro Retention;
data Etape1_RC;
	set Etape1;
	%do i = 1 %to 22;
		%let ip1 = %eval(&i. + 1); 
		if (23 	<= 	(date_rendez_vous_v&i. - Debut) 	<= 44) 		then rv_30days 	= 1;
		if (45 	<= 	(date_rendez_vous_v&i. - Debut)  	<= 74) 		then rv_60days 	= 1;
		if (60 	<= 	(date_rendez_vous_v&i. - Debut)  	<= 134) 	then rv_90days 	= 1; *primary outcome 20170224;
		if (135 <= 	(date_rendez_vous_v&i. - Debut)  	<= 210) 	then rv_180days = 1; *primary outcome 20170224;
		if (275 <= 	(date_rendez_vous_v&i. - Debut)  	<= 456) 	then rv_1yr 	= 1;
		if (457 <= 	(date_rendez_vous_v&i. - Debut)  	<= 638) 	then rv_1yr6 	= 1;
		if (639 <= 	(date_rendez_vous_v&i. - Debut)  	<= 821) 	then rv_2yr 	= 1;
		if (822 <= 	(date_rendez_vous_v&i. - Debut)  	<= 1004) 	then rv_2yr6 	= 1;
		if (1005 <= (date_rendez_vous_v&i. - Debut)  	<= 1187) 	then rv_3yr 	= 1;
		if (1188 <= (date_rendez_vous_v&i. - Debut)  	<= 1368) 	then rv_3yr6 	= 1;

		if (23 	<= 	(date_rendez_vous_v&i. - Debut) 	<= 44) 	 and (-8 < (suivi_visite_v&ip1. - date_rendez_vous_v&i.) < 15) 	then reten_30days 	= 1;
		if (45 	<= 	(date_rendez_vous_v&i. - Debut)  	<= 74) 	 and    abs(suivi_visite_v&ip1. - date_rendez_vous_v&i.) < 15 	then reten_60days 	= 1;
		if (60 	<= 	(date_rendez_vous_v&i. - Debut)  	<= 134)  and    abs(suivi_visite_v&ip1. - date_rendez_vous_v&i.) < 15 	then reten_90days 	= 1;
		if (135 <= 	(date_rendez_vous_v&i. - Debut)  	<= 210)  and    abs(suivi_visite_v&ip1. - date_rendez_vous_v&i.) < 15 	then reten_180days 	= 1;
		if (275 <= 	(date_rendez_vous_v&i. - Debut)  	<= 456)  and    abs(suivi_visite_v&ip1. - date_rendez_vous_v&i.) < 15 	then reten_1yr 		= 1;
		if (457 <= 	(date_rendez_vous_v&i. - Debut)  	<= 638)  and    abs(suivi_visite_v&ip1. - date_rendez_vous_v&i.) < 15 	then reten_1yr6 	= 1;
		if (639 <= 	(date_rendez_vous_v&i. - Debut)  	<= 821)  and    abs(suivi_visite_v&ip1. - date_rendez_vous_v&i.) < 15 	then reten_2yr 		= 1;
		if (822 <= 	(date_rendez_vous_v&i. - Debut)  	<= 1004) and    abs(suivi_visite_v&ip1. - date_rendez_vous_v&i.) < 15 	then reten_2yr6 	= 1;
		if (1005 <= (date_rendez_vous_v&i. - Debut)  	<= 1187) and    abs(suivi_visite_v&ip1. - date_rendez_vous_v&i.) < 15 	then reten_3yr 		= 1;
		if (1188 <= (date_rendez_vous_v&i. - Debut)  	<= 1368) and    abs(suivi_visite_v&ip1. - date_rendez_vous_v&i.) < 15 	then reten_3yr6 	= 1;
	%end;
	Total_Rentetion_Count = sum(reten_30days,reten_60days,reten_90days,reten_180days,reten_1yr,reten_1yr6,reten_2yr,reten_2yr6,reten_3yr,reten_3yr6);
	if Total_Rentetion_Count > 0 then AnyRentain = 1;
		else if Total_Rentetion_Count = 0 then AnyRentain = 0;
	if (date_rendez_vous_v1 ^= . and suivi_visite_v1 ^= .) and reten_30days 	= . then reten_30days 	= 0;
	if (date_rendez_vous_v1 ^= . and suivi_visite_v1 ^= .) and reten_60days 	= . then reten_60days 	= 0;
	if (date_rendez_vous_v1 ^= . and suivi_visite_v1 ^= .) and reten_90days 	= . then reten_90days 	= 0;
	if (date_rendez_vous_v1 ^= . and suivi_visite_v1 ^= .) and reten_180days 	= . then reten_180days 	= 0;
	if (date_rendez_vous_v1 ^= . and suivi_visite_v1 ^= .) and reten_1yr 		= . then reten_1yr 		= 0;
	if (date_rendez_vous_v1 ^= . and suivi_visite_v1 ^= .) and reten_1yr6 		= . then reten_1yr6 	= 0;
	if (date_rendez_vous_v1 ^= . and suivi_visite_v1 ^= .) and reten_2yr 		= . then reten_2yr 		= 0;
	if (date_rendez_vous_v1 ^= . and suivi_visite_v1 ^= .) and reten_2yr6 		= . then reten_2yr6 	= 0;
	if (date_rendez_vous_v1 ^= . and suivi_visite_v1 ^= .) and reten_3yr 		= . then reten_3yr 		= 0;
	if (date_rendez_vous_v1 ^= . and suivi_visite_v1 ^= .) and reten_3yr6 		= . then reten_3yr6 	= 0;

	arv_month = month(Debut);
	arv_year = year(Debut);

	facility_code = substr(ID,1,6);

	if facility_code in('I-CABE' 'I-CPIM' 'I-BETT' 'I-PABE' 'I-HGAG' 'I-TANG' 'I-AKOB' 'I-DOUF') then intervention_id=0;
	if facility_code in('N-CDIM' 'N-KANG' 'N-TIEM' 'N-ANAN' 'N-OUEL' 'N-HGDA' 'N-NDAD' 'N-PMID') then intervention_id=1;

	if 	arv_year < 9999 and arv_year ^= . and arv_month ^= .;
	if arv_month in(1 2 3) then Quarter = 1;
	if arv_month in(4 5 6) then Quarter = 2;
	if arv_month in(7 8 9) then Quarter = 3;
	if arv_month in(10 11 12) then Quarter = 4;
run;
%Mend;

%Retention;

data cdi.ret_avec_rv_20170227;
	set Etape1_RC;
run;






*SECOND OUTCOME MEASURE -- ADHERENCE BASED ON RENDEZVOUS DATE;

libname cdi "S:\Korte\Ivory_Coast_2015\l'analyse des donnees_Hyer\Donnees";

data Etape1;
	set cdi.donnees_nettoyees_20170227jek;
run;

%Macro Retention;
data Etape1_AC;
	set Etape1;
	%do i = 1 %to 21;
		%let ip1 = %eval(&i. + 1); 
		if (23 	<= 	(date_rendez_vous_v&i. - Debut) 	<= 44) 		then rv_30days 	= 1;
		if (45 	<= 	(date_rendez_vous_v&i. - Debut)  	<= 74) 		then rv_60days 	= 1;
		if (60 	<= 	(date_rendez_vous_v&i. - Debut)  	<= 134) 	then rv_90days 	= 1; *primary outcome 20170227;
		if (135 <= 	(date_rendez_vous_v&i. - Debut)  	<= 210) 	then rv_180days = 1; *primary outcome 20170227;
		if (275 <= 	(date_rendez_vous_v&i. - Debut)  	<= 456) 	then rv_1yr 	= 1;
		if (457 <= 	(date_rendez_vous_v&i. - Debut)  	<= 638) 	then rv_1yr6 	= 1;
		if (639 <= 	(date_rendez_vous_v&i. - Debut)  	<= 821) 	then rv_2yr 	= 1;
		if (822 <= 	(date_rendez_vous_v&i. - Debut)  	<= 1004) 	then rv_2yr6 	= 1;
		if (1005 <= (date_rendez_vous_v&i. - Debut)  	<= 1187) 	then rv_3yr 	= 1;
		if (1188 <= (date_rendez_vous_v&i. - Debut)  	<= 1368) 	then rv_3yr6 	= 1;

		if (23 	<= 	(date_rendez_vous_v&i. - Debut) 	<= 44) 		and abs(suivi_visite_v&ip1. - date_rendez_vous_v&i.) < 4 		then adher_30days 	= 1;
		if (45 	<= 	(date_rendez_vous_v&i. - Debut)  	<= 74) 		and abs(suivi_visite_v&ip1. - date_rendez_vous_v&i.) < 4 		then adher_60days 	= 1;
		if (60 	<= 	(date_rendez_vous_v&i. - Debut)  	<= 134) 	and abs(suivi_visite_v&ip1. - date_rendez_vous_v&i.) < 4 		then adher_90days 	= 1;
		if (135 <= 	(date_rendez_vous_v&i. - Debut)  	<= 210) 	and abs(suivi_visite_v&ip1. - date_rendez_vous_v&i.) < 4 		then adher_180days 	= 1; *not correct, if they come early it is OK;
		if (275 <= 	(date_rendez_vous_v&i. - Debut)  	<= 456) 	and abs(suivi_visite_v&ip1. - date_rendez_vous_v&i.) < 4 		then adher_1yr 		= 1;
		if (457 <= 	(date_rendez_vous_v&i. - Debut)  	<= 638) 	and abs(suivi_visite_v&ip1. - date_rendez_vous_v&i.) < 4 		then adher_1yr6 	= 1;
		if (639 <= 	(date_rendez_vous_v&i. - Debut)  	<= 821) 	and abs(suivi_visite_v&ip1. - date_rendez_vous_v&i.) < 4 		then adher_2yr 		= 1;
		if (822 <= 	(date_rendez_vous_v&i. - Debut)  	<= 1004) 	and abs(suivi_visite_v&ip1. - date_rendez_vous_v&i.) < 4 		then adher_2yr6 	= 1;
		if (1005 <= (date_rendez_vous_v&i. - Debut)  	<= 1187) 	and abs(suivi_visite_v&ip1. - date_rendez_vous_v&i.) < 4 		then adher_3yr 		= 1;
		if (1188 <= (date_rendez_vous_v&i. - Debut)  	<= 1368) 	and abs(suivi_visite_v&ip1. - date_rendez_vous_v&i.) < 4 		then adher_3yr6 	= 1;
	%end;
	Total_Rentetion_Count = sum(adher_30days,adher_60days,adher_90days,adher_180days,adher_1yr,adher_1yr6,adher_2yr,adher_2yr6,adher_3yr,adher_3yr6);
	if Total_Rentetion_Count > 0 then AnyRentain = 1;
		else if Total_Rentetion_Count = 0 then AnyRentain = 0;
	if (date_rendez_vous_v1 ^= . and suivi_visite_v1 ^= .) and adher_30days 	= . then adher_30days 	= 0;
	if (date_rendez_vous_v1 ^= . and suivi_visite_v1 ^= .) and adher_60days 	= . then adher_60days 	= 0;
	if (date_rendez_vous_v1 ^= . and suivi_visite_v1 ^= .) and adher_90days 	= . then adher_90days 	= 0;
	if (date_rendez_vous_v1 ^= . and suivi_visite_v1 ^= .) and adher_180days 	= . then adher_180days 	= 0;
	if (date_rendez_vous_v1 ^= . and suivi_visite_v1 ^= .) and adher_1yr 		= . then adher_1yr 		= 0;
	if (date_rendez_vous_v1 ^= . and suivi_visite_v1 ^= .) and adher_1yr6 		= . then adher_1yr6 	= 0;
	if (date_rendez_vous_v1 ^= . and suivi_visite_v1 ^= .) and adher_2yr 		= . then adher_2yr 		= 0;
	if (date_rendez_vous_v1 ^= . and suivi_visite_v1 ^= .) and adher_2yr6 		= . then adher_2yr6 	= 0;
	if (date_rendez_vous_v1 ^= . and suivi_visite_v1 ^= .) and adher_3yr 		= . then adher_3yr 		= 0;
	if (date_rendez_vous_v1 ^= . and suivi_visite_v1 ^= .) and adher_3yr6 		= . then adher_3yr6 	= 0;

	arv_month = month(Debut);
	arv_year = year(Debut);

	facility_code = substr(ID,1,6);

	if facility_code in('I-CABE' 'I-CPIM' 'I-BETT' 'I-PABE' 'I-HGAG' 'I-TANG' 'I-AKOB' 'I-DOUF') then intervention_id=0;
	if facility_code in('N-CDIM' 'N-KANG' 'N-TIEM' 'N-ANAN' 'N-OUEL' 'N-HGDA' 'N-NDAD' 'N-PMID') then intervention_id=1;

	if 	arv_year < 9999 and arv_year ^= . and arv_month ^= .;
	if arv_month in(1 2 3) then Quarter = 1;
	if arv_month in(4 5 6) then Quarter = 2;
	if arv_month in(7 8 9) then Quarter = 3;
	if arv_month in(10 11 12) then Quarter = 4;
run;
%Mend;

%Retention;

data cdi.adher_2202;
	set Etape1_AC;
run;


*THIRD OUTCOME MEASURE:  RETENTION BASED ON TIME SINCE ARV START DATE ONLY;

libname cdi "S:\Korte\Ivory_Coast_2015\l'analyse des donnees_Hyer\Donnees";

data Etape1;
	set cdi.donnees_nettoyees_20170227jek;
run;

%Macro Retention;
data Etape1_RC_Sans_RV;
	set Etape1;
	%do i = 1 %to 22;
		%let ip1 = %eval(&i. + 1); 
		if (23 	<= 	(suivi_visite_v&i. - Debut) 	<= 44) 		then RetSRV_30days 	= 1;
		if (45 	<= 	(suivi_visite_v&i. - Debut)  	<= 74) 		then RetSRV_60days 	= 1;
		if (60 	<= 	(suivi_visite_v&i. - Debut)  	<= 134) 	then RetSRV_90days 	= 1; *primary outcome 20170224;
		if (135 <= 	(suivi_visite_v&i. - Debut)  	<= 210) 	then RetSRV_180days = 1; *primary outcome 20170224;
		if (275 <= 	(suivi_visite_v&i. - Debut)  	<= 456) 	then RetSRV_1yr 	= 1;
		if (457 <= 	(suivi_visite_v&i. - Debut)  	<= 638) 	then RetSRV_1yr6 	= 1;
		if (639 <= 	(suivi_visite_v&i. - Debut)  	<= 821) 	then RetSRV_2yr 	= 1;
		if (822 <= 	(suivi_visite_v&i. - Debut)  	<= 1004) 	then RetSRV_2yr6 	= 1;
		if (1005 <= (suivi_visite_v&i. - Debut)  	<= 1187) 	then RetSRV_3yr 	= 1;
		if (1188 <= (suivi_visite_v&i. - Debut)  	<= 1368) 	then RetSRV_3yr6 	= 1;
	%end;
	Total_Rentetion_Count = sum(RetSRV_30days,RetSRV_60days,RetSRV_90days,RetSRV_180days,RetSRV_1yr,RetSRV_1yr6,RetSRV_2yr,RetSRV_2yr6,RetSRV_3yr,RetSRV_3yr6);
	if Total_Rentetion_Count > 0 then AnyRentain = 1;
		else if Total_Rentetion_Count = 0 then AnyRentain 	= 0;
	if RetSRV_30days 	= . then RetSRV_30days 		= 0;
	if RetSRV_60days 	= . then RetSRV_60days 		= 0;
	if RetSRV_90days 	= . then RetSRV_90days 		= 0;
	if RetSRV_180days 	= . then RetSRV_180days 	= 0;
	if RetSRV_1yr 		= . then RetSRV_1yr 		= 0;
	if RetSRV_1yr6 		= . then RetSRV_1yr6 		= 0;
	if RetSRV_2yr 		= . then RetSRV_2yr 		= 0;
	if RetSRV_2yr6 		= . then RetSRV_2yr6 		= 0;
	if RetSRV_3yr 		= . then RetSRV_3yr 		= 0;
	if RetSRV_3yr6 		= . then RetSRV_3yr6 		= 0;

	arv_month = month(Debut);
	arv_year = year(Debut);

	facility_code = substr(ID,1,6);

	if facility_code in('I-CABE' 'I-CPIM' 'I-BETT' 'I-PABE' 'I-HGAG' 'I-TANG' 'I-AKOB' 'I-DOUF') then intervention_id=0;
	if facility_code in('N-CDIM' 'N-KANG' 'N-TIEM' 'N-ANAN' 'N-OUEL' 'N-HGDA' 'N-NDAD' 'N-PMID') then intervention_id=1;

	if 	arv_year < 9999 and arv_year ^= . and arv_month ^= .;
	if arv_month in(1 2 3) then Quarter = 1;
	if arv_month in(4 5 6) then Quarter = 2;
	if arv_month in(7 8 9) then Quarter = 3;
	if arv_month in(10 11 12) then Quarter = 4;
run;
%Mend;

%Retention;

data cdi.RetSRV_20170227;
	set Etape1_RC_Sans_RV;
run;



*THE NEXT SECTION PRODUCES THE TABLES;

libname cdi "S:\Korte\Ivory_Coast_2015\l'analyse des donnees_Hyer\Donnees";

%let Times = 30days 60days 90days 180days 1yr 1yr6 2yr 2yr6 3yr 3yr6;

options mprint;
%Macro QuarTable(Data, Var_Pre);
%let T = 1;
%do %until(not %length(%scan(&Times,&T)));
	proc freq data = &Data. noprint;
		where rv_%scan(&Times,&T) = 1;
		table arv_year*Quarter*intervention_id*(&Var_Pre._%scan(&Times,&T)) / out = Q&Var_Pre._%scan(&Times,&T)_;
	run;
	data Q&Var_Pre._%scan(&Times,&T)_;
		set Q&Var_Pre._%scan(&Times,&T)_;
		if &Var_Pre._%scan(&Times,&T) ^= .;
	data Q&Var_Pre._%scan(&Times,&T)_Cntrl;
		set Q&Var_Pre._%scan(&Times,&T)_;
		if intervention_id = 0;
		if &Var_Pre._%scan(&Times,&T) = 1;
		&Var_Pre._Cntrl_%scan(&Times,&T) = COUNT;
		keep arv_year Quarter &Var_Pre._Cntrl_%scan(&Times,&T);
	data Q&Var_Pre._%scan(&Times,&T)_Case;
		set Q&Var_Pre._%scan(&Times,&T)_;
		if intervention_id = 1;
		if &Var_Pre._%scan(&Times,&T) = 1;
		&Var_Pre._Case_%scan(&Times,&T) = COUNT;
		keep arv_year Quarter &Var_Pre._Case_%scan(&Times,&T);
	run;
	data QNon_&Var_Pre._%scan(&Times,&T)_Cntrl;
		set Q&Var_Pre._%scan(&Times,&T)_;
		if intervention_id = 0;
		if &Var_Pre._%scan(&Times,&T) = 0;
		Non_&Var_Pre._Cntrl_%scan(&Times,&T) = COUNT;
		keep arv_year Quarter Non_&Var_Pre._Cntrl_%scan(&Times,&T);
	data QNon_&Var_Pre._%scan(&Times,&T)_Case;
		set Q&Var_Pre._%scan(&Times,&T)_;
		if intervention_id = 1;
		if &Var_Pre._%scan(&Times,&T) = 0;
		Non_&Var_Pre._Case_%scan(&Times,&T) = COUNT;
		keep arv_year Quarter Non_&Var_Pre._Case_%scan(&Times,&T);
	run;
	proc sort data = Q&Var_Pre._%scan(&Times,&T)_Cntrl;
		by arv_year Quarter;
	proc sort data = Q&Var_Pre._%scan(&Times,&T)_Case;
		by arv_year Quarter;
	proc sort data = QNon_&Var_Pre._%scan(&Times,&T)_Cntrl;
		by arv_year Quarter;
	proc sort data = QNon_&Var_Pre._%scan(&Times,&T)_Case;
		by arv_year Quarter;
	data Q&Var_Pre._%scan(&Times,&T);
		merge Q&Var_Pre._%scan(&Times,&T)_Cntrl QNon_&Var_Pre._%scan(&Times,&T)_Cntrl Q&Var_Pre._%scan(&Times,&T)_Case QNon_&Var_Pre._%scan(&Times,&T)_Case;
		by arv_year Quarter;
		if &Var_Pre._Cntrl_%scan(&Times,&T) = . then &Var_Pre._Cntrl_%scan(&Times,&T) = 0;
		if &Var_Pre._Case_%scan(&Times,&T) = . then &Var_Pre._Case_%scan(&Times,&T) = 0;
		if Non_&Var_Pre._Cntrl_%scan(&Times,&T) = . then Non_&Var_Pre._Cntrl_%scan(&Times,&T) = 0;
		if Non_&Var_Pre._Case_%scan(&Times,&T) = . then Non_&Var_Pre._Case_%scan(&Times,&T) = 0;
		Total_Cntrl_%scan(&Times,&T) = &Var_Pre._Cntrl_%scan(&Times,&T) + Non_&Var_Pre._Cntrl_%scan(&Times,&T);
		Total_Case_%scan(&Times,&T) = &Var_Pre._Case_%scan(&Times,&T) + Non_&Var_Pre._Case_%scan(&Times,&T);
		Prct_&Var_Pre._Cntrl_%scan(&Times,&T) = 100*(&Var_Pre._Cntrl_%scan(&Times,&T)/Total_Cntrl_%scan(&Times,&T));
		Prct_&Var_Pre._Case_%scan(&Times,&T) = 100*(&Var_Pre._Case_%scan(&Times,&T)/Total_Case_%scan(&Times,&T));
	run;
%let T = %eval(&T + 1);
%end;
data z_Q_Cntrl;
	merge 	%let T = 1;
			%do %until(not %length(%scan(&Times,&T)));
				Q&Var_Pre._%scan(&Times,&T)
			%let T = %eval(&T + 1);
			%end;
			;
	by arv_year Quarter;
	if arv_year < 2018;
	IoC = "  Comparison";
	%let T = 1;
	%do %until(not %length(%scan(&Times,&T)));
		Q&Var_Pre._%scan(&Times,&T) = compbl((&Var_Pre._Cntrl_%scan(&Times,&T)) || "/" || (Total_Cntrl_%scan(&Times,&T)) || " " || "(" || round(Prct_&Var_Pre._Cntrl_%scan(&Times,&T),0.1) || "%)");
		drop 	&Var_Pre._Cntrl_%scan(&Times,&T) Non_&Var_Pre._Cntrl_%scan(&Times,&T) Prct_&Var_Pre._Cntrl_%scan(&Times,&T) Total_Cntrl_%scan(&Times,&T)
				&Var_Pre._Case_%scan(&Times,&T) Non_&Var_Pre._Case_%scan(&Times,&T) Prct_&Var_Pre._Case_%scan(&Times,&T) Total_Case_%scan(&Times,&T)
				;
	%let T = %eval(&T + 1);
	%end;
run;
data z_Q_Case;
	merge 	%let T = 1;
			%do %until(not %length(%scan(&Times,&T)));
				Q&Var_Pre._%scan(&Times,&T)
			%let T = %eval(&T + 1);
			%end;
			;
	by arv_year Quarter;
	if arv_year < 2018;
	IoC = "Intervention";
	%let T = 1;
	%do %until(not %length(%scan(&Times,&T)));
		Q&Var_Pre._%scan(&Times,&T) = compbl((&Var_Pre._Case_%scan(&Times,&T)) || "/" || (Total_Case_%scan(&Times,&T)) || " " || "(" || round(Prct_&Var_Pre._Case_%scan(&Times,&T),0.1) || "%)");
		drop 	&Var_Pre._Cntrl_%scan(&Times,&T) Non_&Var_Pre._Cntrl_%scan(&Times,&T) Prct_&Var_Pre._Cntrl_%scan(&Times,&T) Total_Cntrl_%scan(&Times,&T)
				&Var_Pre._Case_%scan(&Times,&T) Non_&Var_Pre._Case_%scan(&Times,&T) Prct_&Var_Pre._Case_%scan(&Times,&T) Total_Case_%scan(&Times,&T)
				;
	%let T = %eval(&T + 1);
	%end;
run;
data z_QTable_&Var_Pre._Fin;
	set z_Q_Case z_Q_Cntrl;
run;
proc sort data = z_QTable_&Var_Pre._Fin;
	by arv_year Quarter descending IoC;
run;
%Mend;

options mprint;
%Macro MonTable(Data, Var_Pre);
%let T = 1;
%do %until(not %length(%scan(&Times,&T)));
	proc freq data = &Data. noprint;
		where rv_%scan(&Times,&T) = 1;
		table arv_year*arv_month*intervention_id*(&Var_Pre._%scan(&Times,&T)) / out = M&Var_Pre._%scan(&Times,&T)_;
	run;
	data M&Var_Pre._%scan(&Times,&T)_;
		set M&Var_Pre._%scan(&Times,&T)_;
		if &Var_Pre._%scan(&Times,&T) ^= .;
	data M&Var_Pre._%scan(&Times,&T)_Cntrl;
		set M&Var_Pre._%scan(&Times,&T)_;
		if intervention_id = 0;
		if &Var_Pre._%scan(&Times,&T) = 1;
		&Var_Pre._Cntrl_%scan(&Times,&T) = COUNT;
		keep arv_year arv_month &Var_Pre._Cntrl_%scan(&Times,&T);
	data M&Var_Pre._%scan(&Times,&T)_Case;
		set M&Var_Pre._%scan(&Times,&T)_;
		if intervention_id = 1;
		if &Var_Pre._%scan(&Times,&T) = 1;
		&Var_Pre._Case_%scan(&Times,&T) = COUNT;
		keep arv_year arv_month &Var_Pre._Case_%scan(&Times,&T);
	run;
	data MNon_&Var_Pre._%scan(&Times,&T)_Cntrl;
		set M&Var_Pre._%scan(&Times,&T)_;
		if intervention_id = 0;
		if &Var_Pre._%scan(&Times,&T) = 0;
		Non_&Var_Pre._Cntrl_%scan(&Times,&T) = COUNT;
		keep arv_year arv_month Non_&Var_Pre._Cntrl_%scan(&Times,&T);
	data MNon_&Var_Pre._%scan(&Times,&T)_Case;
		set M&Var_Pre._%scan(&Times,&T)_;
		if intervention_id = 1;
		if &Var_Pre._%scan(&Times,&T) = 0;
		Non_&Var_Pre._Case_%scan(&Times,&T) = COUNT;
		keep arv_year arv_month Non_&Var_Pre._Case_%scan(&Times,&T);
	run;
	proc sort data = M&Var_Pre._%scan(&Times,&T)_Cntrl;
		by arv_year arv_month;
	proc sort data = M&Var_Pre._%scan(&Times,&T)_Case;
		by arv_year arv_month;
	proc sort data = MNon_&Var_Pre._%scan(&Times,&T)_Cntrl;
		by arv_year arv_month;
	proc sort data = MNon_&Var_Pre._%scan(&Times,&T)_Case;
		by arv_year arv_month;
	data M&Var_Pre._%scan(&Times,&T);
		merge M&Var_Pre._%scan(&Times,&T)_Cntrl MNon_&Var_Pre._%scan(&Times,&T)_Cntrl M&Var_Pre._%scan(&Times,&T)_Case MNon_&Var_Pre._%scan(&Times,&T)_Case;
		by arv_year arv_month;
		if &Var_Pre._Cntrl_%scan(&Times,&T) = . then &Var_Pre._Cntrl_%scan(&Times,&T) = 0;
		if &Var_Pre._Case_%scan(&Times,&T) = . then &Var_Pre._Case_%scan(&Times,&T) = 0;
		if Non_&Var_Pre._Cntrl_%scan(&Times,&T) = . then Non_&Var_Pre._Cntrl_%scan(&Times,&T) = 0;
		if Non_&Var_Pre._Case_%scan(&Times,&T) = . then Non_&Var_Pre._Case_%scan(&Times,&T) = 0;
		Total_Cntrl_%scan(&Times,&T) = &Var_Pre._Cntrl_%scan(&Times,&T) + Non_&Var_Pre._Cntrl_%scan(&Times,&T);
		Total_Case_%scan(&Times,&T) = &Var_Pre._Case_%scan(&Times,&T) + Non_&Var_Pre._Case_%scan(&Times,&T);
		Prct_&Var_Pre._Cntrl_%scan(&Times,&T) = 100*(&Var_Pre._Cntrl_%scan(&Times,&T)/Total_Cntrl_%scan(&Times,&T));
		Prct_&Var_Pre._Case_%scan(&Times,&T) = 100*(&Var_Pre._Case_%scan(&Times,&T)/Total_Case_%scan(&Times,&T));
	run;
%let T = %eval(&T + 1);
%end;
data z_M_Cntrl;
	merge 	%let T = 1;
			%do %until(not %length(%scan(&Times,&T)));
				M&Var_Pre._%scan(&Times,&T)
			%let T = %eval(&T + 1);
			%end;
			;
	by arv_year arv_month;
	if arv_year < 2018;
	IoC = "  Comparison";
	%let T = 1;
	%do %until(not %length(%scan(&Times,&T)));
		M&Var_Pre._%scan(&Times,&T) = compbl((&Var_Pre._Cntrl_%scan(&Times,&T)) || "/" || (Total_Cntrl_%scan(&Times,&T)) || " " || "(" || round(Prct_&Var_Pre._Cntrl_%scan(&Times,&T),0.1) || "%)");
		drop 	&Var_Pre._Cntrl_%scan(&Times,&T) Non_&Var_Pre._Cntrl_%scan(&Times,&T) Prct_&Var_Pre._Cntrl_%scan(&Times,&T) Total_Cntrl_%scan(&Times,&T)
				&Var_Pre._Case_%scan(&Times,&T) Non_&Var_Pre._Case_%scan(&Times,&T) Prct_&Var_Pre._Case_%scan(&Times,&T) Total_Case_%scan(&Times,&T)
				;
	%let T = %eval(&T + 1);
	%end;
run;
data z_M_Case;
	merge 	%let T = 1;
			%do %until(not %length(%scan(&Times,&T)));
				M&Var_Pre._%scan(&Times,&T)
			%let T = %eval(&T + 1);
			%end;
			;
	by arv_year arv_month;
	if arv_year < 2018;
	IoC = "Intervention";
	%let T = 1;
	%do %until(not %length(%scan(&Times,&T)));
		M&Var_Pre._%scan(&Times,&T) = compbl((&Var_Pre._Case_%scan(&Times,&T)) || "/" || (Total_Case_%scan(&Times,&T)) || " " || "(" || round(Prct_&Var_Pre._Case_%scan(&Times,&T),0.1) || "%)");
		drop 	&Var_Pre._Cntrl_%scan(&Times,&T) Non_&Var_Pre._Cntrl_%scan(&Times,&T) Prct_&Var_Pre._Cntrl_%scan(&Times,&T) Total_Cntrl_%scan(&Times,&T)
				&Var_Pre._Case_%scan(&Times,&T) Non_&Var_Pre._Case_%scan(&Times,&T) Prct_&Var_Pre._Case_%scan(&Times,&T) Total_Case_%scan(&Times,&T)
				;
	%let T = %eval(&T + 1);
	%end;
run;
data z_MTable_&Var_Pre._Fin;
	set z_M_Case z_M_Cntrl;
run;
proc sort data = z_MTable_&Var_Pre._Fin;
	by arv_year arv_month descending IoC;
run;
%Mend;

data Ad_Avec_RV_Quar;
*	set cdi.ad_avec_rv_2202;
	set cdi.ret_avec_rv_20170227;
	if (arv_year in (2013 2014 2015) or (arv_year=2016 and arv_month<7));
*	if (arv_month < 7 and arv_year = 2015) or arv_year < 2015;
run;
data Ad_Avec_RV_Mon;
*	set cdi.ad_avec_rv_2202;
	set cdi.ret_avec_rv_20170227;
	if ((arv_month >= 7 and arv_year = 2015) or (arv_year=2016 and arv_month<7));
*	if arv_month >= 7 and arv_year = 2015;
run;

ods rtf file="S:\Korte\Ivory_Coast_2015\final report\madison results\reten_rdv_20170227_all.rtf";
%QuarTable(Ad_Avec_RV_Quar, reten);
%MonTable(Ad_Avec_RV_Mon, reten);
proc print
	 data=z_MTable_reten_Fin noobs;
	 var arv_year	arv_month	IoC	Mreten_90days	Mreten_180days	 	Mreten_1yr		;
	 run;
proc print
	 data=z_QTable_reten_Fin noobs;
	 var arv_year	Quarter	IoC	Qreten_90days	Qreten_180days
	 	Qreten_1yr         Qreten_1yr6        Qreten_2yr Qreten_2yr6        Qreten_3yr         Qreten_3yr6
		;
	 run;
ods rtf close;

ods rtf file="S:\Korte\Ivory_Coast_2015\final report\madison results\reten_rdv_20170227.rtf";
%QuarTable(Ad_Avec_RV_Quar, reten);
%MonTable(Ad_Avec_RV_Mon, reten);
proc print
	 data=z_MTable_reten_Fin;
	 var arv_year arv_month IoC Mreten_90days Mreten_180days;
	 run;
proc print
	 data=z_QTable_reten_Fin;
	 var arv_year quarter IoC Qreten_90days Qreten_180days;
	 run;
ods rtf close;


data Adher_RV_Quar;
	set cdi.adher_2202;
	if (arv_year in (2013 2014 2015) or (arv_year=2016 and arv_month<7));
run;
data Adher_RV_Mon;
	set cdi.adher_2202;
	if ((arv_month >= 7 and arv_year = 2015) or (arv_year=2016 and arv_month<7));
run;

%QuarTable(Adher_RV_Quar, adher);
%MonTable(Adher_RV_Mon, adher);
proc print data = z_MTable_adher_Fin;
run;
proc print data = z_QTable_adher_Fin;
run;

data RetSRV_Quar;
	set cdi.RetSRV_20170227;
	if (arv_year in (2013 2014 2015) or (arv_year=2016 and arv_month<7));
	rv_30days 	= 1;
	rv_60days 	= 1;
	rv_90days 	= 1;
	rv_180days 	= 1;
	rv_1yr 		= 1;
	rv_1yr6 	= 1;
	rv_2yr 		= 1;
	rv_2yr6 	= 1;
	rv_3yr 		= 1;
	rv_3yr6 	= 1;
run;
data RetSRV_Mon;
	set cdi.RetSRV_20170227;
	if ((arv_month >= 7 and arv_year = 2015) or (arv_year=2016 and arv_month<7));
	rv_30days 	= 1;
	rv_60days 	= 1;
	rv_90days 	= 1;
	rv_180days 	= 1;
	rv_1yr 		= 1;
	rv_1yr6 	= 1;
	rv_2yr 		= 1;
	rv_2yr6 	= 1;
	rv_3yr 		= 1;
	rv_3yr6 	= 1;
run;

%QuarTable(RetSRV_Quar, RetSRV);
%MonTable(RetSRV_Mon, RetSRV);

proc contents
	 data=z_MTable_RetSRV_Fin;
	 run;
proc contents
	 data=z_QTable_RetSRV_Fin;
	 run;

ods rtf file="S:\Korte\Ivory_Coast_2015\final report\madison results\retSRV_20170227.rtf";
proc print data = z_MTable_RetSRV_Fin noobs;
	var arv_year arv_month IoC MRetSRV_90days MRetSRV_180days
		/*MRetSRV_1yr*/;
	where (((arv_year=2015) and (arv_month>6)) or ((arv_year=2016) and (arv_month <7)));
run;
proc print data = z_QTable_RetSRV_Fin noobs;
	var arv_year Quarter IoC QRetSRV_90days QRetSRV_180days
		/*QRetSRV_1yr	QRetSRV_1yr6	QRetSRV_2yr	QRetSRV_2yr6	QRetSRV_3yr	QRetSRV_3yr6*/;
	where ((arv_year in (2013 2014 2015)) or ((arv_year=2016) and (quarter <3)));
run;
ods rtf close;

ods rtf file="S:\Korte\Ivory_Coast_2015\final report\madison results\retSRV_all_20170227.rtf";
proc print data = z_MTable_RetSRV_Fin;
	where (((arv_year=2015) and (arv_month>6)) or ((arv_year=2016) and (arv_month <7)));
run;
proc print data = z_QTable_RetSRV_Fin;
	where ((arv_year in (2013 2014 2015)) or ((arv_year=2016) and (quarter <3)));
run;
ods rtf close;






* HERE IS CODE TO GENERATE THE DEMOGRAPHICS RESULTS;
libname cdi "S:\Korte\Ivory_Coast_2015\l'analyse des donnees_Hyer\Donnees";

data Etape1;
	set cdi.donnees_nettoyees_20170227jek;
run;




proc contents data=Etape1;
run;
proc print data=Etape1;
	var ID debut;
	run;

data Etape2;
set Etape1;
if (mdy(12,31,2012)<= debut <= mdy(07,01,2016));
 
run;


data Etape1_20170216;
	set Etape1;
facility_code=substr(ID,1,6); 

if facility_code='I-DOUF' then facility_code_id=1;
if facility_code='I-AKOB' then facility_code_id=2; 
if facility_code='I-HGAG' then facility_code_id=3;
if facility_code='I-BETT' then facility_code_id=4;
if facility_code='I-CABE' then facility_code_id=5;
if facility_code='I-PABE' then facility_code_id=6;
if facility_code='I-TANG' then facility_code_id=7;
if facility_code='I-CPIM' then facility_code_id=8;

if facility_code='N-KANG' then facility_code_id=9;
if facility_code='N-TIEM' then facility_code_id=10;
if facility_code='N-HGDA' then facility_code_id=11;
if facility_code='N-OUEL' then facility_code_id=12;
if facility_code='N-CDIM' then facility_code_id=13;
if facility_code='N-PMID' then facility_code_id=14;
if facility_code='N-ANAN' then facility_code_id=15;
if facility_code='N-NDAD' then facility_code_id=16;

intervention_id=.;
if ((facility_code = 'I-CABE') or (facility_code='I-CPIM') or (facility_code='I-BETT') or (facility_code='I-PABE') or (facility_code='I-HGAG') 
or (facility_code='I-TANG') or (facility_code='I-AKOB') or (facility_code='I-DOUF')) then intervention_id=0;

if ((facility_code='N-CDIM') or (facility_code='N-KANG') or (facility_code='N-TIEM') or (facility_code='N-ANAN') or (facility_code='N-OUEL') 
or (facility_code='N-HGDA') or (facility_code='N-NDAD') or (facility_code='N-PMID')) then intervention_id=1;	


education=.;
if (niveau_dinstruction_1='--blank--') then education=.;    

if ((niveau_dinstruction_1='non scolarise(e)') or (niveau_dinstruction_1='Non scolarise') or (niveau_dinstruction_1='Non Scolarise') 
or (niveau_dinstruction_1=1)) then education=1; /*none*/

if ((niveau_dinstruction_1='primaire') or (niveau_dinstruction_1='Primaire') or (niveau_dinstruction_1=2)) then education=2;
  
if ((niveau_dinstruction_1='secondaire') or (niveau_dinstruction_1='Secondaire') or (niveau_dinstruction_1=3)) then education=3;
/*secondary*/

if ((niveau_dinstruction_1='Superieur') or (niveau_dinstruction_1='Superieurw') or (niveau_dinstruction_1='superieur') or
(niveau_dinstruction_1=4)) then education=4;  /*post-secondary*/
 
if (niveau_dinstruction_1=99) then education=99; 


religion=.;
if (religion_1='--blank--') then religion=.; 
if ((religion_1='Chretien') or (religion_1=1)) then religion=1; 
if ((religion_1='Musulman') or (religion_1=2)) then religion=2; 
if ((religion_1='animiste') or (religion_1='Animiste') or (religion_1=3)) then religion=3;
if ((religion_1='Autre Religion') or (religion_1=4)) then religion=4; 
if ((religion_1='sans religion') or (religion_1='Sans Religion') or (religion_1=5)) then religion=5;
if  (religion_1=99) then education=99;


profession=.;
if (profession_1='--blank--') then profession=.;
if ((profession_1='En Activite')     or (profession_1='En activite')   or (profession_1=1)) then profession=1; 
if ((profession_1='Au Chomage')      or (profession_1='Au chomage')    or (profession_1=2)) then profession=2;
if ((profession_1='a la retraite')   or (profession_1='A la retraite') or (profession_1=3)) then profession=3;
if  (profession_1=4)  then profession=4;  /*student*/
if  (profession_1=99) then profession=99; 
   

/*Code demographics missings and unknowns as missing*/
age_cat=.;
	if ((age LE 19) and (age GE 15)) then age_cat=1;
	if ((age LE 29) and (age GE 20)) then age_cat=2;
	if ((age LE 49) and (age GE 30)) then age_cat=3;
	if ((age LT 65) and (age GE 50)) then age_cat=4;
	if                  (age GE 65)  then age_cat=5;

matri_cat2=.;
	  if ((matri=1) or (matri=3) or (matri=4) or (matri=7)) then matri_cat2=1; 
				/*  Vie en couple,  Marie(e), Veuf, Concubinage */
	  if ((matri=2) or (matri=5) or (matri=6) or (matri=8)) then matri_cat2=2;  
	 			/* Pas en couple, Divorced, Separe, Celibataire- Single */
	  if matri=99 then matri_cat2=99;

matri_cat6=.;
	  if ((matri=1) or (matri=7)) then matri_cat6=1; /*Living together- Vie en couple, Concubinage*/   
 	  if ((matri=2) or (matri=8)) then matri_cat6=2;	/* Pas en couple, Celibataire- Single */
      if matri=3 then matri_cat6=3; /*married- marie*/
      if matri=4 then matri_cat6=4; /*widowed- veuf*/ 
	  if matri=5 then matri_cat6=5; /*divorced- Divorc&#233(e)*/
  	/*  if matri_1_dc=6 then matri_1_dc_cat6=6; /*separated- Separ&#233- No one reported being separated*/
	  if matri=99 then matri_cat6=99;

	cd4_jour0_=cd4_jour0;
	if ((cd4_jour0=9999) or (cd4_jour0=.)) then cd4_jour0_=.;

	poids_kg_=poids_kg; 
	if ((poids_kg=999) or (poids_kg=.)) then poids_kg_=.;

	cdc_stage_part=.;
	if cdc_partie1=1 and cdc_partie2=1 then cdc_stage_part=1;
	if cdc_partie1=1 and cdc_partie2=2 then cdc_stage_part=2;
	if cdc_partie1=1 and cdc_partie2=3 then cdc_stage_part=3;

	if cdc_partie1=2 and cdc_partie2=1 then cdc_stage_part=4;
	if cdc_partie1=2 and cdc_partie2=2 then cdc_stage_part=5;
	if cdc_partie1=2 and cdc_partie2=3 then cdc_stage_part=6;

	if cdc_partie1=3 and cdc_partie2=1 then cdc_stage_part=7;
	if cdc_partie1=3 and cdc_partie2=2 then cdc_stage_part=8;
	if cdc_partie1=3 and cdc_partie2=3 then cdc_stage_part=9;

	if cdc_partie1=99 or cdc_partie2=99 then cdc_stage_part=99;   
   
run;


proc freq data=Etape1_20170216;
	table facility_code_id intervention_id annee_testvih age_cat matri_cat6 cdc_stage_part cd4_jour0_ facility_code;
	run;


data Etape1_20170216_1;
set Etape1_20170216;

resultat_vih_sida=.;
if (resultat_vih_sida_1='--blank--') then resultat_vih_sida=.; 
if ((resultat_vih_sida_1='VIH 1')    or (resultat_vih_sida_1='VIH-1')   or (resultat_vih_sida_1=1)) then resultat_vih_sida=1; /*VIH 1*/ 
if ((resultat_vih_sida_1='VIH 2')    or (resultat_vih_sida_1='VIH-2')   or (resultat_vih_sida_1=2)) then resultat_vih_sida=2; /*VIH 2*/
if ((resultat_vih_sida_1='VIH dual') or (resultat_vih_sida_1='VIH 1+2') or (resultat_vih_sida_1=3)) then resultat_vih_sida=3;  /*VIH dual*/
if ((resultat_vih_sida_1='Non precise') or (resultat_vih_sida_1=4)) then resultat_vih_sida=4;  /*non precise*/
if  (resultat_vih_sida_1=99) then resultat_vih_sida=99;

if ((resultat_vih_sida=1) or (resultat_vih_sida=2) or (resultat_vih_sida=3)) then 
years_living_hiv=(2016 - annee_testvih);

years_living_hiv_cat=.;
	if ((years_living_hiv<1)  and (years_living_hiv ne .)) then years_living_hiv_cat=1;
	if ((years_living_hiv>=1) and (years_living_hiv<=5))   then years_living_hiv_cat=2;
	if ((years_living_hiv>5)  and (years_living_hiv<98))   then years_living_hiv_cat=3; 

contraception=.;
if (contraception_1='--blank--') then contraception=.;
if ((contraception_1='non') or (contraception_1='Non') or (contraception_1=0)) then contraception=0; /*no*/
if ((contraception_1='Oui') or (contraception_1='oui') or (contraception_1=1)) then contraception=1;  /*yes*/
if ((contraception_1='Non Applicable') or (contraception_1=9)) then contraception=9;  /*pas applicable*/
if  (contraception_1=99) then contraception=99;


tuberculose=.; 
if  (tuberculose_1='--blank--') then tuberculose=.;
if ((tuberculose_1='Non') or (tuberculose_1=0)) then tuberculose=0; /*no tb*/
if ((tuberculose_1='Oui') or (tuberculose_1=1)) then tuberculose=1; /*yes tb*/
if ((tuberculose_1='Ne Sait Pas') or (tuberculose_1='Ne sait pas') or (tuberculose_1=2)) then tuberculose=2;  
if  (tuberculose_1=99) then tuberculose=99;  

point_dentre_soins=.;
if (point_dentre_soins_1='--blank--') then point_dentre_soins=.;
if ((point_dentre_soins_1='CDV') or (point_dentre_soins_1=1)) then point_dentre_soins=1;
if ((point_dentre_soins_1='PTME') or (point_dentre_soins_1=2)) then point_dentre_soins=2;
if ((point_dentre_soins_1='Medecine') or (point_dentre_soins_1=3)) then point_dentre_soins=3;
if ((point_dentre_soins_1='Pediatrie') or (point_dentre_soins_1=4)) then point_dentre_soins=4;
if ((point_dentre_soins_1='CAT ou CDT ') or (point_dentre_soins_1=5)) then point_dentre_soins=5;
if ((point_dentre_soins_1='Hospitalisation') or (point_dentre_soins_1=6)) then point_dentre_soins=6;
if ((point_dentre_soins_1='Auto Reference') or (point_dentre_soins_1='Auto reference') or (point_dentre_soins_1=7)) then point_dentre_soins=7;
if (point_dentre_soins_1=8) then point_dentre_soins=8;  /*OBC*/
if (point_dentre_soins_1=9) then point_dentre_soins=9;  /*Toxicology*/
if (point_dentre_soins_1=10) then point_dentre_soins=10;  /*IST*/
if (point_dentre_soins_1=11) then point_dentre_soins=11;  /*AES*/
if ((point_dentre_soins_1='Autres') or (point_dentre_soins_1=12)) then point_dentre_soins=12;
if  (point_dentre_soins_1=99) then point_dentre_soins=99;

grossesse_cours=.;
if  (grossesse_cours_1='--blank--') then grossesse_cours=.; 
if ((grossesse_cours_1='Non') or (grossesse_cours_1=0)) then grossesse_cours=0; 
if ((grossesse_cours_1='Oui') or (grossesse_cours_1=1)) then grossesse_cours=1;
if ((grossesse_cours_1='Non Applicable') or (grossesse_cours_1=9)) then grossesse_cours=9;
if  (grossesse_cours_1=99) then grossesse_cours=99;

arv=.;
if (arv_1='--blank--') then arv=.;   
if ((arv_1='Non') or (arv=0)) then arv=0;
if ((arv_1='Oui') or (arv=1)) then arv=1;
if (arv_1=99) then arv=99; 


beneficie_ptme=.; 
if  (beneficie_ptme_1='--blank--') then beneficie_ptme=.;
if ((beneficie_ptme_1='Ne sait pas') or (beneficie_ptme_1=2)) the beneficie_ptme=2;
if ((beneficie_ptme_1='Non') or (beneficie_ptme_1=0)) then beneficie_ptme=0;
if ((beneficie_ptme_1='Non Applicable') or (beneficie_ptme_1='Non applicable') or (beneficie_ptme_1=9)) then beneficie_ptme_1=9; /*pas applicable*/
if ((beneficie_ptme_1='Oui') or (beneficie_ptme_1=1)) then beneficie_ptme=1;
if (beneficie_ptme_1=99) then beneficie_ptme=99;

run;



*****************************************************
*Table 1 - Char. at Enrollment into Care.  2-16-2017*
*****************************************************/;
/*Demographic table presents n(%) for all categories including missings (9, 99, 999, etc.), and p-values from chi square test*/ 


/*Totals for Table 1*/
proc sort 
	data=Etape1_20170216_1;
	by id intervention_id;
	run;
proc freq data=Etape1_20170216_1;
table id intervention_id;
run;



/*Sex*/
proc sort
	data=Etape1_20170216_1;
	by id feminin;
	run;
proc freq data=Etape1_20170216_1;
table feminin*intervention_id / chisq  exact;
run;

proc freq data=Etape1_20170216_1;
table feminin*intervention_id /missing ;
run;



/*Years since HIV categories*/
proc sort
	data=Etape1_20170216_1;
	by id years_living_hiv_cat;
	run;
proc freq data=Etape1_20170216_1;
table years_living_hiv_cat*intervention_id /chisq  exact ;
run;
proc freq data=Etape1_20170216_1;
table years_living_hiv_cat*intervention_id /missing  ;
run;


/*Age categories*/
proc sort
	data=Etape1_20170216_1;
	by id age_cat;
	run;
proc freq data=Etape1_20170216_1;
table age_cat*intervention_id /chisq ;
run;
proc freq data=Etape1_20170216_1;
table age_cat*intervention_id /missing  ;
run;


/*Level of education*/
proc sort
	data=Etape1_20170216_1;
	by id education;
	run;
proc freq data=Etape1_20170216_1;
table education *intervention_id /chisq ;
run;
proc freq data=Etape1_20170216_1;
table education *intervention_id /missing  ;
run;


/*Religion*/
proc sort
	data=Etape1_20170216_1;
	by id religion;
	run;
proc freq data=Etape1_20170216_1;
table religion *intervention_id /chisq  exact;
run;
proc freq data=Etape1_20170216_1;
table religion *intervention_id /missing ;
run;


/*Marital status categories*/
proc sort 
	data=Etape1_20170216_1;
	by id matri_cat6;
	run;
proc freq data=Etape1_20170216_1;
table matri_cat6*intervention_id /chisq ;
run;
proc freq data=Etape1_20170216_1;
table matri_cat6*intervention_id /missing;
run;


/*Employment*/
proc sort
	data=Etape1_20170216_1;
	by id profession;
	run;
proc freq data=Etape1_20170216_1;
table profession *intervention_id /chisq  exact;
run;
proc freq data=Etape1_20170216_1;
table profession *intervention_id /missing ;
run;


/*Contraception*/
proc sort 
	data=Etape1_20170216_1;
	by id contraception;
	run; 
proc freq data=Etape1_20170216_1;
table contraception *intervention_id /chisq ;
run;
proc freq data=Etape1_20170216_1;
table contraception *intervention_id /missing ;
run;


/*HIV test result*/
proc sort data=Etape1_20170216_1;
	by id resultat_vih_sida;
	run;
proc freq data=Etape1_20170216_1;
table resultat_vih_sida *intervention_id /chisq exact;
run;
proc freq data=Etape1_20170216_1;
table resultat_vih_sida *intervention_id /missing;
run;


/*TB test result*/
proc sort
	data=Etape1_20170216_1;
	by id tuberculose;
	run; 
proc freq data=Etape1_20170216_1;
table tuberculose *intervention_id /chisq  ;
run;
proc freq data=Etape1_20170216_1;
table tuberculose *intervention_id /missing  ;
run;


/*Point_dentre_soins*/
proc sort
	data=Etape1_20170216_1;
	by id point_dentre_soins;
	run;  
proc freq data=Etape1_20170216_1;
table point_dentre_soins *intervention_id /chisq  ;
run;
proc freq data=Etape1_20170216_1;
table point_dentre_soins *intervention_id /missing ;
run;


/*Prior ARV */
proc sort
	data=Etape1_20170216_1;
	by id arv;
	run; 
proc freq data=Etape1_20170216_1;
table arv *intervention_id /chisq  ;
run;
proc freq data=Etape1_20170216_1;
table arv *intervention_id /missing ;
run;


/*Grossesse - pregnancy */
proc sort
	data=Etape1_20170216_1;
	by id grossesse_cours;
	run; 
proc freq data=Etape1_20170216_1;
table grossesse_cours *intervention_id /chisq  ;
run;
proc freq data=Etape1_20170216_1;
table grossesse_cours *intervention_id /missing ;
run;


/*Beneficie PTME */
proc sort
	data=Etape1_20170216_1;
	by id beneficie_ptme;
	run; 
proc freq data=Etape1_20170216_1;
table beneficie_ptme *intervention_id /chisq exact ;
run;
proc freq data=Etape1_20170216_1;
table beneficie_ptme *intervention_id /missing ;
run;


/*Weight (kg) */
proc sort data=Etape1_20170216_1;
by id poids_kg_;  run;

proc univariate data=Etape1_20170216_1;
var poids_kg_; 
run;
proc means
	 data=Etape1_20170216_1;
	 var poids_kg poids_kg_;
	 run;
proc ttest
	 data=Etape1_20170216_1;
	 class intervention_id;
	 var poids_kg_;
	 run;

proc freq data=Etape1_20170216_1;
table poids_kg; run;

proc univariate data=Etape1_20170216_1;
var poids_kg_; 
by intervention_id;
run;

proc freq data=Etape1_20170216_1;
table poids_kg_;
run;

/*Missing and no response by intervention*/
proc freq data=Etape1_20170216_1;
table poids_kg;
by intervention_id;
run;


/*CD 4 - cd4_jour0_1 (MISSINGS EXCLUDED)*/
proc sort
	data=Etape1_20170216_1;
	by id cd4_jour0_;
	run; 
proc univariate data=Etape1_20170216_1;
var cd4_jour0_; run;
proc means
	 data=Etape1_20170216_1;
	 var cd4_jour0_ cd4_jour0; 
	 run;
proc ttest
	 data=Etape1_20170216_1;
	 class intervention_id;
	 var cd4_jour0_;
	 run;

proc freq data=Etape1_20170216_1;
table cd4_jour0; run;

proc univariate  data=Etape1_20170216_1;
var cd4_jour0_; 
by intervention_id;
run;

/*Missing and no response by intervention*/
proc freq data=Etape1_20170216_1;
table cd4_jour0; 
by intervention_id; run;





*************************************
*Table 2 - Participants by Facility *
*************************************;
proc sort 
	data=Etape1_20170216_1;
	by id facility_code_id intervention_id;
	run;
proc print
	 data=Etape1_20170216_1;
	 var facility_code_id intervention_id;
	 title "";
	 run; 

proc freq data=Etape1_20170216_1;
table facility_code_id*intervention_id;
where intervention_id=0;
run;

proc freq data=Etape1_20170216_1;
table facility_code_id*intervention_id;
where intervention_id=1;
run;

proc freq data=Etape1_20170216_1;
table facility_code_id*intervention_id;
run;








*LOGISTIC REGRESSION MODELING;
data temp;
*	 set cdi.RetSRV_20170227;  *has outcomes;
	 set Etape1_20170216_1;
run;
data outcome;
	 set cdi.RetSRV_20170227;
	 keep id RetSRV_90days RetSRV_180days;
	 run;
proc contents
	 data=temp;
	 run;
proc freq
	 data=temp;
	 tables feminin 
			years_living_hiv_cat 
			age_cat 
			religion
			profession
			contraception
			;
	 run;

proc sort
	 data=temp;
	 by id;
	 run;
proc sort
	 data=outcome;
	 by id;
	 run;
data temp2;
	 merge temp outcome;
	 by id;
	 run;
proc contents
	 data=temp2;
	 run;			*there are 2519 total;
data monthly;
	 set temp2;
	 if mdy(07,01,2015) <= debut < mdy(07,01,2016);
	 run;


proc logistic
	 data=temp2
	 descending;
	 class /*years_living_hiv_cat*/ religion(ref=first) profession(ref=first)
			contraception(ref=first);
	 model RetSRV_90days=intervention_id /*  intervention=1, comparison=0  */	 
						 ;
	 run;
proc logistic
	 data=temp2
	 descending;
	 class /*years_living_hiv_cat*/ religion(ref=first) profession(ref=first)
			contraception(ref=first);
	 model RetSRV_90days=intervention_id /*  intervention=1, comparison=0  */	 
						 cd4_jour0_
						 cd4_jour0_*intervention_id
						 ;
	 run;
proc logistic
	 data=temp2
	 descending;
	 class /*years_living_hiv_cat*/ religion(ref=first) profession(ref=first)
			contraception(ref=first);
	 model RetSRV_90days=intervention_id /*  intervention=1, comparison=0  */
						 feminin
						 years_living_hiv_cat
						 age_cat
						 religion
						 /*profession*/
						 contraception
						 cd4_jour0_

						 /*intervention_id*feminin*/
						 /*intervention_id*years_living_hiv_cat*/
						 /*intervention_id*age_cat*/
						 intervention_id*cd4_jour0_
						 ;
	 run;									***NO INTERACTION SEEN BY AGE, SEX, OR YRS WITH HIV;
proc logistic
	 data=temp2
	 descending;
	 model RetSRV_180days=intervention_id
						  feminin
						  ;
	 run;




*propensity score matching analysis;


data psm30;
	 set Etape1_20170216_1;
	 if (mdy(01,01,2016)<= debut <= mdy(06,30,2016));
	 run;
data psm90;
	 set Etape1_20170216_1;
	 if (mdy(11,01,2015)<= debut <= mdy(03,31,2016));
	 run;
data psm180;
	 set Etape1_20170216_1;
	 if (mdy(07,01,2015)<= debut <= mdy(12,31,2015));
	 run;


proc export 
	 data=psm30 
	 outfile= "S:\Korte\Ivory_Coast_2015\propensity score matching\psm30.dta" replace;
	 run;
proc export 
	 data=psm90 
	 outfile= "S:\Korte\Ivory_Coast_2015\propensity score matching\psm90.dta" replace;
	 run;
proc export 
	 data=psm180 
	 outfile= "S:\Korte\Ivory_Coast_2015\propensity score matching\psm180.dta" replace;
	 run;
proc export 
	 data=Etape1_20170216_1 
	 outfile= "S:\Korte\Ivory_Coast_2015\propensity score matching\demog20170602.dta" replace;
	 run;



*run PSMATCH2 function in STATA;
*then import matched datasets back into SAS;

proc import 
	 out=psm.psm30_20170613_old
	 datafile='S:\Korte\Ivory_Coast_2015\propensity score matching\psm30_20170613_old.dta'
	 dbms=DTA replace;
	 run;
proc import 
	 out=psm.psm90_20170614_old
	 datafile='S:\Korte\Ivory_Coast_2015\propensity score matching\psm90_20170614_old.dta'
	 dbms=DTA replace;
	 run;
proc import 
	 out=psm.psm180_20170614_old
	 datafile='S:\Korte\Ivory_Coast_2015\propensity score matching\psm180_20170614_old.dta'
	 dbms=DTA replace;
	 run;



data psm_id_30;
	 set psm.psm30_20170613_old;
	 keep id;
	 run; *keep only these individuals for the PSM 30 day analyses;
data psm_id_90;
	 set psm.psm90_20170614_old;
	 keep id;
	 run; *keep only these individuals for the PSM 90 day analyses;
data psm_id_180;
	 set psm.psm180_20170614_old;
	 keep id;
	 run; *keep only these individuals for the PSM 180 day analyses;






proc sort data=psm_id_30; by id; run;
proc sort data=psm_id_90; by id; run;
proc sort data=psm_id_180; by id; run;
proc sort data=PSM_ID; by id; run;
proc sort data=Etape1_RC; by id; run;
*30 day outcome;
data psm_ret_rdv30;
	 merge psm_id_30(in=a) Etape1_RC(in=b);
	 by id;
	 if a=1;

	 after_int=.;
	  if 			   .<debut<mdy(02,01,2016) then after_int=0;
	  if mdy(02,01,2016)<debut 				   then after_int=1;
	 run;



proc freq
	 data=psm_ret_rdv30;
	 tables intervention_id*reten_30days
			/chisq;
	 title "30 days - main PSM analysis";
	 run;



*90 day outcome;
data psm_ret_rdv90;
	 merge psm_id_90(in=a) Etape1_RC(in=b);
	 by id;
	 if a=1;

	 after_int=.;
	  if 			   .<debut<mdy(02,01,2016) then after_int=0;
	  if mdy(02,01,2016)<debut 				   then after_int=1;
	 run;

proc freq
	 data=psm_ret_rdv90;
	 tables intervention_id*reten_90days
			/chisq;
	 title "90 days: main PSM analysis";
	 run;




*180 day outcome;
data psm_ret_rdv180;
	 merge psm_id_180(in=a) Etape1_RC(in=b);
	 by id;
	 if a=1;

	 after_int=.;
	  if 			   .<debut<mdy(02,01,2016) then after_int=0;
	  if mdy(02,01,2016)<debut 				   then after_int=1;
	 run;


proc freq
	 data=psm_ret_rdv180;
	 tables intervention_id*reten_180days
			/chisq;
	 title "180 days - main PSM analysis";
	 run;











